package com.emmy.walnut.presenter;

import android.os.Handler;
import android.os.Message;

import com.emmy.walnut.app.AppDatabase;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.Record;
import com.emmy.walnut.model.RecordSortHelperModel;
import com.emmy.walnut.model.RecordWithPeriod;
import com.emmy.walnut.presenter.iface.StatisticsPIface;
import com.emmy.walnut.util.LiveDataBus;
import com.emmy.walnut.view.iface.StatisticsIface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

public class StatisticsPresenter implements StatisticsPIface {

    public StatisticsIface statisticsIface;

    public StatisticsPresenter(StatisticsIface statisticsIface){
        this.statisticsIface = statisticsIface;
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what== AppConstant.GET_PEROID_RECORD){
                statisticsIface.updateData((List<Record>) msg.obj);
                return true;
            }
            return false;
        }
    });

    @Override
    public void queryRecordWithPeriod(final long startTimestamp, final long endTimestamp) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<Record> records =
                        AppDatabase.getInstance().recordDao().getRecordWithPeriod(
                                AppConstant.username, startTimestamp, endTimestamp);
                Message.obtain(handler, AppConstant.GET_PEROID_RECORD, records).sendToTarget();
            }
        }).start();
    }

}
